<template>
  <div>
    <a-radio-group v-model="value" @change="onChange">
      <a-radio-button :value="0"> 大屏展示 </a-radio-button>
      <a-radio-button :value="2"> 真实数据 </a-radio-button>
      <a-radio-button :value="1"> 自定义数据 </a-radio-button>
    </a-radio-group>
    <div v-if="value === 0">
      <a-card class="_mt-20 br-10" :bordered="false" title="会员数据统计">
        <a-table bordered :data-source="memberData" :columns="memberClumn" :pagination="false">
          <template slot="area" slot-scope="text, records">
            <span>
              <span style="margin-right: 5px">{{ text }}</span>
              <a-popconfirm
                v-if="records.carry.custom === 1"
                placement="bottomRight"
                ok-text="恢复"
                cancel-text="取消"
                @confirm="handleMemberStatistics(records.carry)"
              >
                <template slot="title">
                  <p>真实数据</p>
                  <a-table
                    :data-source="records.carry.data"
                    :columns="headerDel(memberClumn)"
                    :pagination="false"
                  ></a-table>
                </template>
                <a-tag style="cursor: pointer" color="#f50">自定义</a-tag>
              </a-popconfirm>

              <a-tag v-else color="#87d068">真数据</a-tag>
            </span>
          </template>
        </a-table>
      </a-card>
      <a-card class="_mt-20 br-10" :bordered="false" title="职务数据统计">
        <a-table bordered :data-source="levelData" :columns="levelClumn" :pagination="false">
          <template slot="area" slot-scope="text, records">
            <span>
              <span style="margin-right: 5px">{{ text }}</span>
              <a-popconfirm
                v-if="records.carry.custom === 1"
                placement="bottomRight"
                ok-text="恢复"
                cancel-text="取消"
                @confirm="handlelevelStatistics(records.carry)"
              >
                <template slot="title">
                  <p>真实数据</p>
                  <a-table
                    :data-source="records.carry.data"
                    :columns="headerDel(levelClumn)"
                    :pagination="false"
                  ></a-table>
                </template>
                <a-tag style="cursor: pointer" color="#f50">自定义</a-tag>
              </a-popconfirm>
              <a-tag v-else color="#87d068">真数据</a-tag>
            </span>
          </template>
        </a-table>
      </a-card>
      <a-card class="_mt-20 br-10" :bordered="false" title="类别数据统计">
        <a-table bordered :data-source="typeData" :columns="typeClumn" :pagination="false">
          <template slot="area" slot-scope="text, records">
            <span>
              <span style="margin-right: 5px">{{ text }}</span>
              <a-popconfirm
                v-if="records.carry.custom === 1"
                placement="bottomRight"
                ok-text="恢复"
                cancel-text="取消"
                @confirm="handletypeStatistics(records.carry)"
              >
                <template slot="title">
                  <p>真实数据</p>
                  <a-table
                    :data-source="records.carry.data"
                    :columns="headerDel(typeClumn)"
                    :pagination="false"
                  ></a-table>
                </template>
                <a-tag style="cursor: pointer" color="#f50">自定义</a-tag>
              </a-popconfirm>
              <a-tag v-else color="#87d068">真数据</a-tag>
            </span>
          </template>
        </a-table>
      </a-card>
      <a-card class="_mt-20 br-10" :bordered="false" title="行业数据统计">
        <a-table bordered :data-source="industryData" :columns="industryClumn" :pagination="false">
          <template slot="area" slot-scope="text, records">
            <span>
              <span style="margin-right: 5px">{{ text }}</span>
              <a-popconfirm
                v-if="records.carry.custom === 1"
                placement="bottomRight"
                ok-text="恢复"
                cancel-text="取消"
                @confirm="handleindustryStatistics(records.carry)"
              >
                <template slot="title">
                  <p>真实数据</p>
                  <a-table
                    :data-source="records.carry.data"
                    :columns="headerDel(industryClumn)"
                    :pagination="false"
                  ></a-table>
                </template>
                <a-tag style="cursor: pointer" color="#f50">自定义</a-tag>
              </a-popconfirm>
              <a-tag v-else color="#87d068">真数据</a-tag>
            </span>
          </template>
        </a-table>
      </a-card>
    </div>
    <div v-else-if="value === 2">
      <a-card class="_mt-20 br-10" :bordered="false" title="会员数据统计">
        <div class="btn-right" slot="extra">
          <a-button size="small" class="btn-style" @click="handleEdit('id1')">自定义数据</a-button>
        </div>
        <a-table bordered :data-source="memberTrueData" :columns="memberTrueClumn" :pagination="false"></a-table>
      </a-card>
      <a-card class="_mt-20 br-10" :bordered="false" title="职务数据统计">
        <div class="btn-right" slot="extra">
          <a-button size="small" class="btn-style" @click="handleEdit('id2')">自定义数据</a-button>
        </div>
        <a-table bordered :data-source="levelTrueData" :columns="levelTrueClumn" :pagination="false"></a-table>
      </a-card>
      <!-- <a-card class="_mt-20 br-10" :bordered="false" title="评级数据统计">
        <div class="btn-right" slot="extra">
          <a-button size="small" class="btn-style" @click="handleEdit('id3')">自定义数据</a-button>
        </div>
        <a-table bordered :data-source="ratingTrueData" :columns="ratingTrueClumn" :pagination="false"></a-table>
      </a-card> -->
      <a-card class="_mt-20 br-10" :bordered="false" title="类别数据统计">
        <div class="btn-right" slot="extra">
          <a-button size="small" class="btn-style" @click="handleEdit('id4')">自定义数据</a-button>
        </div>
        <a-table bordered :data-source="typeTrueData" :columns="typeTrueClumn" :pagination="false"></a-table>
      </a-card>
      <a-card class="_mt-20 br-10" :bordered="false" title="行业数据统计">
        <div class="btn-right" slot="extra">
          <a-button size="small" class="btn-style" @click="handleEdit('id5')">自定义数据</a-button>
        </div>
        <a-table bordered :data-source="industryTrueData" :columns="industryTrueClumn" :pagination="false"></a-table>
      </a-card>
    </div>
    <div v-else-if="value === 1">
      <a-card class="_mt-20 br-10" :bordered="false" title="会员数据统计" id="id1">
        <div class="btn-right" slot="extra">
          <a-button size="small" class="btn-style" @click="handlememberUpdCustom">保存</a-button>
          <!--<a-button size="small" class="btn-style" type="primary" @click="handlememberPublishData">发布</a-button>-->
        </div>
        <a-table bordered :data-source="cusmemberData" :columns="cusmemberClumn" :pagination="false">
          <template v-for="(item, idx) in cusmemberClumn" :slot="item.dataIndex" slot-scope="text, records">
            <span :key="idx" v-if="item.dataIndex !== 'action'">
              <a-input-number
                style="width: 100%"
                :min="0"
                v-model="records[item.dataIndex]"
                @change="handlememberChande(records)"
              ></a-input-number>
            </span>
            <span :key="idx" v-else-if="item.dataIndex === 'action'">
              <a @click="handlememberPublishData([records])">发布</a>
            </span>
          </template>
        </a-table>
      </a-card>
      <a-card class="_mt-20 br-10" :bordered="false" title="职务数据统计" id="id2">
        <div class="btn-right" slot="extra">
          <a-button size="small" class="btn-style" @click="handlelevelUpdCustom">保存</a-button>
          <!--<a-button size="small" class="btn-style" type="primary" @click="handlelevelPublishDataValidate">发布</a-button>-->
        </div>
        <a-table bordered :data-source="cuslevelData" :columns="cuslevelClumn" :pagination="false">
          <template v-for="(item, idx) in cuslevelClumn" :slot="item.dataIndex" slot-scope="text, records, index">
            <span :key="idx" v-if="item.dataIndex !== 'action'">
              <a-input-number
                style="width: 100%"
                :min="0"
                v-model="records[item.dataIndex]"
                @change="handlelevelChande(records)"
              ></a-input-number>
            </span>
            <span :key="idx" v-else-if="item.dataIndex === 'action'">
              <a @click="handlelevelPublishDataValidate([records], index)">发布</a>
            </span>
          </template>
        </a-table>
      </a-card>
      <a-card class="_mt-20 br-10" :bordered="false" title="类别数据统计" id="id4">
        <div class="btn-right" slot="extra">
          <a-button size="small" class="btn-style" @click="handletypeUpdCustom">保存</a-button>
          <!--<a-button size="small" class="btn-style" type="primary" @click="handletypePublishDataValidate">发布</a-button>-->
        </div>
        <a-table bordered :data-source="custypeData" :columns="custypeClumn" :pagination="false">
          <template v-for="(item, idx) in custypeClumn" :slot="item.dataIndex" slot-scope="text, records, index">
            <span :key="idx" v-if="item.dataIndex !== 'action'">
              <a-input-number
                style="width: 100%"
                :min="0"
                v-model="records[item.dataIndex]"
                @change="handletypeChande(records)"
              ></a-input-number>
            </span>
            <span :key="idx" v-else-if="item.dataIndex === 'action'">
              <a @click="handletypePublishDataValidate([records], index)">发布</a>
            </span>
          </template>
        </a-table>
      </a-card>
      <a-card class="_mt-20 br-10" :bordered="false" title="行业数据统计" id="id5">
        <div class="btn-right" slot="extra">
          <a-button size="small" class="btn-style" @click="handleindustryUpdCustom">保存</a-button>
          <!--<a-button size="small" class="btn-style" type="primary" @click="handleindustryPublishDataValidate">发布</a-button>-->
        </div>
        <a-table bordered :data-source="cusindustryData" :columns="cusindustryClumn" :pagination="false">
          <template v-for="(item, idx) in cusindustryClumn" :slot="item.dataIndex" slot-scope="text, records, index">
            <span :key="idx" v-if="item.dataIndex !== 'action'">
              <a-input-number
                style="width: 100%"
                :min="0"
                v-model="records[item.dataIndex]"
                @change="handleindustryChande(records)"
              ></a-input-number>
            </span>
            <span :key="idx" v-else-if="item.dataIndex === 'action'">
              <a @click="handleindustryPublishDataValidate([records], index)">发布</a>
            </span>
          </template>
        </a-table>
      </a-card>
    </div>
  </div>
</template>
<script>
import {
  memberRenderDtl,
  memberStatistics,
  memberRenderCustomDtl,
  memberUpdCustom,
  memberPublishData,
  levelRenderDtl,
  levelStatistics,
  levelRenderCustomDtl,
  levelUpdCustom,
  levelPublishData,
  ratingRenderDtl,
  ratingStatistics,
  ratingRenderCustomDtl,
  ratingUpdCustom,
  ratingPublishData,
  typeRenderDtl,
  typeStatistics,
  typeRenderCustomDtl,
  typeUpdCustom,
  typePublishData,
  industryRenderDtl,
  industryStatistics,
  industryRenderCustomDtl,
  industryUpdCustom,
  industryPublishData,
  memberRenderRealDtl,
  levelRenderRealDtl,
  ratingRenderRealDtl,
  typeRenderRealDtl,
  industryRenderRealDtl,
} from '@/api/web/biz'

export default {
  data() {
    return {
      value: 0,
      memberData: [],
      memberClumn: [],
      cusmemberData: [],
      defcusmemberData: [],
      cusmemberClumn: [],
      defcusmemberClumn: [],
      memberTrueClumn: [],
      memberTrueData: [],

      levelData: [],
      levelClumn: [],
      cuslevelData: [],
      defcuslevelData: [],
      cuslevelClumn: [],
      defcuslevelClumn: [],
      levelTrueClumn: [],
      levelTrueData: [],

      ratingData: [],
      ratingClumn: [],
      cusratingData: [],
      defcusratingData: [],
      cusratingClumn: [],
      defcusratingClumn: [],
      ratingTrueClumn: [],
      ratingTrueData: [],

      typeData: [],
      typeClumn: [],
      custypeData: [],
      defcustypeData: [],
      custypeClumn: [],
      defcustypeClumn: [],
      typeTrueClumn: [],
      typeTrueData: [],

      industryData: [],
      industryClumn: [],
      cusindustryData: [],
      defcusindustryData: [],
      cusindustryClumn: [],
      defcusindustryClumn: [],
      industryTrueClumn: [],
      industryTrueData: [],
      totalKey: undefined,
    }
  },
  created() {
    this.getData()
  },
  methods: {
    getData() {
      this.handleMemberData()
      this.handlelevelData()
      this.handleratingData()
      this.handletypeData()
      this.handleindustryData()
    },

    getCusData() {
      this.handlememberRenderCustomDtl()
      this.handlelevelRenderCustomDtl()
      this.handleratingRenderCustomDtl()
      this.handletypeRenderCustomDtl()
      this.handleindustryRenderCustomDtl()
    },

    getTrueData() {
      this.handleMemberTrueData()
      this.handlelevelTrueData()
      this.handleratingTrueData()
      this.handletypeTrueData()
      this.handleindustryTrueData()
    },

    handleEdit(id) {
      this.value = 1
      this.$nextTick(() => {
        document.getElementById(id).scrollIntoView()
      })
    },
    // 会员统计
    handleMemberData() {
      return memberRenderDtl().then((res) => {
        if (res.code === 0) {
          this.memberClumn = this.headerInit(res.data.header, 'name', 'key')
          this.memberData = this.dataInit1(res.data.cell, 'key', 'value', true)
        }
      })
    },

    // 会员真实数据
    handleMemberTrueData() {
      return memberRenderRealDtl().then((res) => {
        if (res.code === 0) {
          this.memberTrueClumn = this.headerInit(res.data.header, 'name', 'key')
          this.memberTrueData = this.dataInit1(res.data.cell, 'key', 'value')
        }
      })
    },

    // 会员数据恢复
    handleMemberStatistics(obj) {
      var params = {
        areaId: obj.areaId,
      }
      return memberStatistics(params).then((res) => {
        if (res.code === 0) {
          this.$message.success('恢复成功')
          this.handleMemberData()
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 会员自定义统计
    handlememberRenderCustomDtl() {
      return memberRenderCustomDtl().then((res) => {
        if (res.code === 0) {
          this.defcusmemberClumn = JSON.parse(JSON.stringify(res.data.header))
          this.cusmemberClumn = this.cusheaderInit(res.data.header, 'name', 'key')
          this.cusmemberData = this.dataInit(res.data.cell, 'key', 'value')
          this.defcusmemberData = this.dataInit(res.data.cell, 'key', 'value')

          // 获取会员统计的key
          for (var item of res.data.header) {
            if (item.name === '会员总数') {
              this.totalKey = item.key
            }
          }
        }
      })
    },

    // 会员自定义数据保存
    handlememberUpdCustom() {
      var _this = this
      this.$confirm({
        title: '是否确定保存?',
        onOk() {
          var data = {
            header: _this.defcusmemberClumn,
            cell: _this.arrToObj(_this.cusmemberData, 'key', 'value'),
          }
          return memberUpdCustom(data).then((res) => {
            if (res.code === 0) {
              _this.$message.success('保存成功')
              _this.handlememberRenderCustomDtl()
            } else {
              _this.$message.error(res.msg)
            }
          })
        },
        onCancel() {},
      })
    },

    // 会员自定义数据发布
    handlememberPublishData(arr) {
      var data = {
        header: this.defcusmemberClumn,
        cell: this.arrToObj(arr, 'key', 'value'),
      }
      return memberPublishData(data).then((res) => {
        if (res.code === 0) {
          this.$message.success('发布成功')
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 修改会员数据
    handlememberChande(obj) {
      var total = 0
      for (var item in obj) {
        if (item !== 'total' && item !== '-1') {
          total += Number(obj[item])
        }
      }
      obj.total = total
    },

    // 职务数据统计
    handlelevelData() {
      return levelRenderDtl().then((res) => {
        if (res.code === 0) {
          this.levelClumn = this.headerInit(res.data.header, 'name', 'key')
          this.levelData = this.dataInit(res.data.cell, 'key', 'value', true)
        }
      })
    },

    // 职务真实数据
    handlelevelTrueData() {
      return levelRenderRealDtl().then((res) => {
        if (res.code === 0) {
          this.levelTrueClumn = this.headerInit(res.data.header, 'name', 'key')
          this.levelTrueData = this.dataInit(res.data.cell, 'key', 'value')
        }
      })
    },

    // 职务数据恢复
    handlelevelStatistics(obj) {
      var params = {
        areaId: obj.areaId,
      }
      return levelStatistics(params).then((res) => {
        if (res.code === 0) {
          this.$message.success('恢复成功')
          this.handlelevelData()
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 职务自定义统计
    handlelevelRenderCustomDtl() {
      return levelRenderCustomDtl().then((res) => {
        if (res.code === 0) {
          this.defcuslevelClumn = JSON.parse(JSON.stringify(res.data.header))
          this.cuslevelClumn = this.cusheaderInit(res.data.header, 'name', 'key')
          this.cuslevelData = this.dataInit(res.data.cell, 'key', 'value')
          this.defcuslevelData = this.dataInit(res.data.cell, 'key', 'value')
        }
      })
    },

    // 职务自定义数据保存校验
    handlelevelUpdCustom() {
      var msg = ''
      var _this = this
      /* return levelUpdCustom().then(res => {}) */
      var flag = true
      this.cuslevelData.map((item, index) => {
        if (item.total !== this.defcusmemberData[index][this.totalKey]) {
          msg += `${item['-1']}的职务数据统计与${this.defcusmemberData[index]['-1']}的会员总数不一致,`
          flag = false
        }
      })
      msg += '是否确定保存?'
      if (!flag) {
        this.$confirm({
          title: msg,
          onOk() {
            _this.handlelevelUpd()
          },
          onCancel() {},
        })
      } else {
        this.$confirm({
          title: '是否确定保存?',
          onOk() {
            _this.handlelevelUpd()
          },
          onCancel() {},
        })
      }
    },

    // 职务自定义数据保存
    handlelevelUpd() {
      var data = {
        header: this.defcuslevelClumn,
        cell: this.arrToObj(this.cuslevelData, 'key', 'value'),
      }
      return levelUpdCustom(data).then((res) => {
        if (res.code === 0) {
          this.$message.success('保存成功')
          this.handlelevelRenderCustomDtl()
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 自定义上传数据校验
    handlelevelPublishDataValidate(arr, index) {
      var msg = ''
      /* return levelUpdCustom().then(res => {}) */
      var flag = true
      arr.map((item) => {
        if (item.total !== this.defcusmemberData[index][this.totalKey]) {
          msg += `${item['-1']}的职务数据统计与${this.defcusmemberData[index]['-1']}的会员总数不一致,`
          flag = false
        }
      })
      msg += '是否确定保存?'
      if (!flag) {
        var _this = this
        this.$confirm({
          title: msg,
          onOk() {
            _this.handlelevelPublishData(arr)
          },
          onCancel() {},
        })
      } else {
        this.handlelevelPublishData(arr)
      }
    },

    // 自定义职务数据上传
    handlelevelPublishData(arr) {
      var data = {
        header: this.defcuslevelClumn,
        cell: this.arrToObj(arr, 'key', 'value'),
      }
      return levelPublishData(data).then((res) => {
        if (res.code === 0) {
          this.$message.success('发布成功')
          this.handlelevelRenderCustomDtl()
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 修改职务数据
    handlelevelChande(obj) {
      var total = 0
      for (var item in obj) {
        if (item !== 'total' && item !== '-1') {
          total += Number(obj[item])
        }
      }
      obj.total = total
    },

    // 评级统计
    handleratingData() {
      return ratingRenderDtl().then((res) => {
        if (res.code === 0) {
          this.ratingClumn = this.headerInit(res.data.header, 'name', 'key')
          this.ratingData = this.dataInit(res.data.cell, 'key', 'value', true)
        }
      })
    },

    // 评级真实数据
    handleratingTrueData() {
      return ratingRenderRealDtl().then((res) => {
        if (res.code === 0) {
          this.ratingTrueClumn = this.headerInit(res.data.header, 'name', 'key')
          this.ratingTrueData = this.dataInit(res.data.cell, 'key', 'value')
        }
      })
    },

    // 评级数据恢复
    handleratingStatistics(obj) {
      var params = {
        areaId: obj.areaId,
      }
      return ratingStatistics(params).then((res) => {
        if (res.code === 0) {
          this.$message.success('恢复成功')
          this.handleratingData()
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 评级自定义统计
    handleratingRenderCustomDtl() {
      return ratingRenderCustomDtl().then((res) => {
        if (res.code === 0) {
          this.defcusratingClumn = JSON.parse(JSON.stringify(res.data.header))
          this.cusratingClumn = this.cusheaderInit(res.data.header, 'name', 'key')
          this.cusratingData = this.dataInit(res.data.cell, 'key', 'value')
          this.defcusratingData = this.dataInit(res.data.cell, 'key', 'value')
        }
      })
    },

    // 评级自定义数据保存校验
    handleratingUpdCustom() {
      var msg = ''
      var flag = true
      var _this = this
      this.cusratingData.map((item, index) => {
        if (item.total !== this.defcusmemberData[index][this.totalKey]) {
          msg += `${item['-1']}的评级数据统计与${this.defcusmemberData[index]['-1']}的会员总数不一致,`
          flag = false
        }
      })
      msg += '是否确定保存?'
      if (!flag) {
        this.$confirm({
          title: msg,
          onOk() {
            _this.handleratingUpd()
          },
          onCancel() {},
        })
      } else {
        this.$confirm({
          title: '是否确定保存?',
          onOk() {
            _this.handleratingUpd()
          },
          onCancel() {},
        })
      }
    },

    // 评级自定义数据保存
    handleratingUpd() {
      var data = {
        header: this.defcusratingClumn,
        cell: this.arrToObj(this.cusratingData, 'key', 'value'),
      }
      return ratingUpdCustom(data).then((res) => {
        if (res.code === 0) {
          this.$message.success('保存成功')
          this.handleratingRenderCustomDtl()
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 自定义上传数据校验
    handleratingPublishDataValidate(arr, index) {
      var msg = ''
      /* return levelUpdCustom().then(res => {}) */
      var flag = true
      arr.map((item) => {
        if (item.total !== this.defcusmemberData[index][this.totalKey]) {
          msg += `${item['-1']}的评级数据统计与${this.defcusmemberData[index]['-1']}的会员总数不一致,`
          flag = false
        }
      })
      msg += '是否确定保存?'
      if (!flag) {
        var _this = this
        this.$confirm({
          title: msg,
          onOk() {
            _this.handleratingPublishData(arr)
          },
          onCancel() {},
        })
      } else {
        this.handleratingPublishData(arr)
      }
    },

    // 自定义评级数据上传
    handleratingPublishData(arr) {
      var data = {
        header: this.defcusratingClumn,
        cell: this.arrToObj(arr, 'key', 'value'),
      }
      return ratingPublishData(data).then((res) => {
        if (res.code === 0) {
          this.$message.success('发布成功')
          this.handleratingRenderCustomDtl()
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 修改评级数据
    handleratingChande(obj) {
      var total = 0
      for (var item in obj) {
        if (item !== 'total' && item !== '-1') {
          total += Number(obj[item])
        }
      }
      obj.total = total
    },

    // 类型统计
    handletypeData() {
      return typeRenderDtl().then((res) => {
        if (res.code === 0) {
          this.typeClumn = this.headerInit(res.data.header, 'name', 'key')
          this.typeData = this.dataInit(res.data.cell, 'key', 'value', true)
        }
      })
    },

    // 类型真实数据
    handletypeTrueData() {
      return typeRenderRealDtl().then((res) => {
        if (res.code === 0) {
          this.typeTrueClumn = this.headerInit(res.data.header, 'name', 'key')
          this.typeTrueData = this.dataInit(res.data.cell, 'key', 'value')
        }
      })
    },

    // 类型数据恢复
    handletypeStatistics(obj) {
      var params = {
        areaId: obj.areaId,
      }
      return typeStatistics(params).then((res) => {
        if (res.code === 0) {
          this.$message.success('恢复成功')
          this.handletypeData()
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 类型自定义统计
    handletypeRenderCustomDtl() {
      return typeRenderCustomDtl().then((res) => {
        if (res.code === 0) {
          this.defcustypeClumn = JSON.parse(JSON.stringify(res.data.header))
          this.custypeClumn = this.cusheaderInit(res.data.header, 'name', 'key')
          this.custypeData = this.dataInit(res.data.cell, 'key', 'value')
          this.defcustypeData = this.dataInit(res.data.cell, 'key', 'value')
        }
      })
    },

    // 类型自定义数据保存校验
    handletypeUpdCustom() {
      var msg = ''
      var _this = this
      /* return levelUpdCustom().then(res => {}) */
      var flag = true
      this.custypeData.map((item, index) => {
        if (item.total !== this.defcusmemberData[index][this.totalKey]) {
          msg += `${item['-1']}的评级数据统计与${this.defcusmemberData[index]['-1']}的会员总数不一致,`
          flag = false
          return false
        }
      })
      msg += '是否确定保存?'
      if (!flag) {
        this.$confirm({
          title: msg,
          onOk() {
            _this.handletypeUpd()
          },
          onCancel() {},
        })
      } else {
        this.$confirm({
          title: '是否确定保存?',
          onOk() {
            _this.handletypeUpd()
          },
          onCancel() {},
        })
      }
    },

    // 类型自定义数据保存
    handletypeUpd() {
      var data = {
        header: this.defcustypeClumn,
        cell: this.arrToObj(this.custypeData, 'key', 'value'),
      }
      return typeUpdCustom(data).then((res) => {
        if (res.code === 0) {
          this.$message.success('保存成功')
          this.handletypeRenderCustomDtl()
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 自定义上传数据校验
    handletypePublishDataValidate(arr, index) {
      var msg = ''
      /* return levelUpdCustom().then(res => {}) */
      var flag = true
      arr.map((item) => {
        if (item.total !== this.defcusmemberData[index][this.totalKey]) {
          msg += `${item['-1']}的评级数据统计与${this.defcusmemberData[index]['-1']}的会员总数不一致,`
          flag = false
          return false
        }
      })
      msg += '是否确定保存?'
      if (!flag) {
        var _this = this
        this.$confirm({
          title: msg,
          onOk() {
            _this.handletypePublishData(arr)
          },
          onCancel() {},
        })
      } else {
        this.handletypePublishData(arr)
      }
    },

    // 自定义类型数据上传
    handletypePublishData(arr) {
      var data = {
        header: this.defcustypeClumn,
        cell: this.arrToObj(arr, 'key', 'value'),
      }
      return typePublishData(data).then((res) => {
        if (res.code === 0) {
          this.$message.success('发布成功')
          this.handletypeRenderCustomDtl()
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 修改类型数据
    handletypeChande(obj) {
      var total = 0
      for (var item in obj) {
        if (item !== 'total' && item !== '-1') {
          total += Number(obj[item])
        }
      }
      obj.total = total
    },

    // 行业统计
    handleindustryData() {
      return industryRenderDtl().then((res) => {
        if (res.code === 0) {
          this.industryClumn = this.headerInit(res.data.header, 'name', 'key')
          this.industryData = this.dataInit(res.data.cell, 'key', 'value', true)
        }
      })
    },

    // 行业真实数据
    handleindustryTrueData() {
      return industryRenderRealDtl().then((res) => {
        if (res.code === 0) {
          this.industryTrueClumn = this.headerInit(res.data.header, 'name', 'key')
          this.industryTrueData = this.dataInit(res.data.cell, 'key', 'value')
        }
      })
    },

    // 行业数据恢复
    handleindustryStatistics(obj) {
      var params = {
        areaId: obj.areaId,
      }
      return industryStatistics(params).then((res) => {
        if (res.code === 0) {
          this.$message.success('恢复成功')
          this.handleindustryData()
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 行业自定义统计
    handleindustryRenderCustomDtl() {
      return industryRenderCustomDtl().then((res) => {
        if (res.code === 0) {
          this.defcusindustryClumn = JSON.parse(JSON.stringify(res.data.header))
          this.cusindustryClumn = this.cusheaderInit(res.data.header, 'name', 'key')
          this.cusindustryData = this.dataInit(res.data.cell, 'key', 'value')
          this.defcusindustryData = this.dataInit(res.data.cell, 'key', 'value')
        }
      })
    },

    // 行业自定义数据保存校验
    handleindustryUpdCustom() {
      var msg = ''
      var _this = this
      /* return levelUpdCustom().then(res => {}) */
      var flag = true
      this.cusindustryData.map((item, index) => {
        if (item.total !== this.defcusmemberData[index][this.totalKey]) {
          msg += `${item['-1']}的行业数据统计与${this.defcusmemberData[index]['-1']}的会员总数不一致,`
          flag = false
        }
      })
      msg += '是否确定保存?'
      if (!flag) {
        this.$confirm({
          title: msg,
          onOk() {
            _this.handleindustryUpd()
          },
          onCancel() {},
        })
      } else {
        this.$confirm({
          title: '是否确定保存?',
          onOk() {
            _this.handleindustryUpd()
          },
          onCancel() {},
        })
      }
    },

    // 行业自定义数据保存
    handleindustryUpd() {
      var data = {
        header: this.defcusindustryClumn,
        cell: this.arrToObj(this.cusindustryData, 'key', 'value'),
      }
      return industryUpdCustom(data).then((res) => {
        if (res.code === 0) {
          this.$message.success('保存成功')
          this.handleindustryRenderCustomDtl()
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 自定义上传数据校验
    handleindustryPublishDataValidate(arr, index) {
      var msg = ''
      /* return levelUpdCustom().then(res => {}) */
      var flag = true
      arr.map((item) => {
        if (item.total !== this.defcusmemberData[index][this.totalKey]) {
          msg += `${item['-1']}的行业数据统计与${this.defcusmemberData[index]['-1']}的会员总数不一致,`
          flag = false
        }
      })
      msg += '是否确定保存?'
      if (!flag) {
        var _this = this
        this.$confirm({
          title: msg,
          onOk() {
            _this.handleindustryPublishData(arr)
          },
          onCancel() {},
        })
      } else {
        this.handleindustryPublishData(arr)
      }
    },

    // 自定义行业数据上传
    handleindustryPublishData(arr) {
      var data = {
        header: this.defcusindustryClumn,
        cell: this.arrToObj(arr, 'key', 'value'),
      }
      return industryPublishData(data).then((res) => {
        if (res.code === 0) {
          this.$message.success('发布成功')
          this.handleindustryRenderCustomDtl()
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 修改行业数据
    handleindustryChande(obj) {
      var total = 0
      for (var item in obj) {
        if (item !== 'total' && item !== '-1') {
          total += Number(obj[item])
        }
      }
      obj.total = total
    },

    // 表头格式化
    headerInit(arr, title, key, flag) {
      var newArr = JSON.parse(JSON.stringify(arr))
      for (var item of newArr) {
        item['title'] = item[title]
        item['dataIndex'] = item[key]
        item['align'] = 'center'
        item['width'] = '120px'
        if (item[key] === -1) {
          item['scopedSlots'] = { customRender: 'area' }
        }
      }
      var obj = {
        title: '合计',
        width: '100px',
        dataIndex: 'total',
        align: 'center',
      }
      newArr.push(obj)
      if (flag) {
        newArr = newArr.shift()
      }
      return newArr
    },

    // 自定义表头格式化
    cusheaderInit(arr, title, key) {
      var newArr = JSON.parse(JSON.stringify(arr))
      for (var item of newArr) {
        item['title'] = item[title]
        item['dataIndex'] = item[key]
        item['align'] = 'center'
        item['width'] = '120px'
        if (item[key] !== -1) {
          item['scopedSlots'] = { customRender: item[key] }
        }
      }
      var obj = {
        title: '合计',
        width: '100px',
        dataIndex: 'total',
        align: 'center',
      }
      newArr.push(obj)
      var obj1 = {
        title: '操作',
        dataIndex: 'action',
        width: '80px',
        align: 'center',
        scopedSlots: { customRender: 'action' },
      }
      newArr.push(obj1)
      return newArr
    },

    // 数据格式化
    dataInit(arr, key, value, flag) {
      var newArr = []
      for (var item of arr) {
        var obj = {
          total: 0,
        }
        for (var it of item) {
          if (it[key] !== -1) {
            obj.total += it[value]
          } else if (it[key] === -1 && it['carry']) {
            it['carry'].data = []
            if (flag && it['carry'].custom === 1) {
              var obj1 = {
                total: 0,
              }
              for (var it1 of it['carry'].realData) {
                if (it1[key] !== -1) {
                  obj1.total += it1[value]
                }
                obj1[it1[key].toString()] = it1[value]
              }
              it['carry'].data.push(obj1)
            }
            obj.carry = it['carry']
          }
          obj[it[key].toString()] = it[value]
        }
        newArr.push(obj)
      }
      return newArr
    },

    // 数据格式化
    dataInit1(arr, key, value, flag) {
      var newArr = []
      for (var item of arr) {
        var obj = {
          total: 0,
        }
        for (var it of item) {
          if (it[key] !== -1 && it[key] !== 1 && it[key] !== 2) {
            obj.total += it[value]
          } else if (it[key] === -1 && it['carry']) {
            it['carry'].data = []
            if (flag && it['carry'].custom === 1) {
              var obj1 = {
                total: 0,
              }
              for (var it1 of it['carry'].realData) {
                if (it1[key] !== -1 && it1[key] !== 1 && it1[key] !== 2) {
                  obj1.total += it1[value]
                }
                obj1[it1[key].toString()] = it1[value]
              }
              it['carry'].data.push(obj1)
            }
            obj.carry = it['carry']
          }
          obj[it[key].toString()] = it[value]
        }
        newArr.push(obj)
      }
      return newArr
    },

    // 数据转成数组对象
    arrToObj(arr, key, value) {
      var newArr = []
      arr.map((item) => {
        var itemArr = []
        for (const i in item) {
          var obj = {}
          if (i !== 'total') {
            obj[key] = Number(i)
            obj[value] = item[i]
            itemArr.push(obj)
          }
        }
        newArr.push(itemArr)
      })
      return newArr
    },

    // 删除区
    headerDel(arr) {
      var newArr = JSON.parse(JSON.stringify(arr))
      newArr.shift()
      return newArr
    },

    onChange(e) {
      if (e.target.value === 0) {
        this.getData()
      } else if (e.target.value === 1) {
        this.getCusData()
      } else {
        this.getTrueData()
      }
    },
  },
}
</script>
<style scoped lang="less">
</style>
